package cs5226.actions;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;

import cs5226.dao.BaselineDAO;
import cs5226.dao.ConfigurationDAO;


public class ConfigBaselineAction  extends ActionSupport implements  ServletRequestAware {
 
	private static final long serialVersionUID = 1L;

	static Log log = LogFactory.getLog(ConfigBaselineAction.class);	
	
	private HttpServletRequest request = null;

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
	}
	
	public String processBaselinesConfiHome() throws Exception {
		
		try {
			
			request.setAttribute("baselinesList", ConfigurationDAO.getBaselines());
			
			request.setAttribute("snapshotIDList",ConfigurationDAO.getSnapshotIDList());
			
		} catch (Exception e) {
//			e.printStackTrace();
//			request.setAttribute("error", e.getMessage());
		}

		return super.SUCCESS;
	}
	
	
	public String createBaselines() throws Exception {		
		try {
			String startID = request.getParameter("startID");
			String endID = request.getParameter("endID");
			String baselineNM = request.getParameter("baselineNM");
			
			ConfigurationDAO.createBaselines(startID, endID, baselineNM);

			request.setAttribute("baselinesList", ConfigurationDAO.getBaselines());
			request.setAttribute("snapshotIDList",ConfigurationDAO.getSnapshotIDList());
			request.setAttribute("msg", "Baseline has been updated successfully. ");
		} catch (Exception e) {
			request.setAttribute("baselinesList", ConfigurationDAO.getBaselines());
			request.setAttribute("snapshotIDList",ConfigurationDAO.getSnapshotIDList());
			e.printStackTrace();
			request.setAttribute("msg", "Exception in createBaselines. "+e.getMessage());
			return super.ERROR;					
		}

		return super.SUCCESS;
	}
	
	
	public String deleteBaselines() throws Exception {		
		try {

			String baselineNM = request.getParameter("baselineNMDel");
			
			ConfigurationDAO.deleteBaselines(baselineNM);
			request.setAttribute("baselinesList", ConfigurationDAO.getBaselines());
			request.setAttribute("snapshotIDList",ConfigurationDAO.getSnapshotIDList());
			request.setAttribute("msg", "Thresholds have been deleted successfully. ");
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("msg", "Exception in deleteBaselines. "+e.getMessage());
			request.setAttribute("baselinesList", ConfigurationDAO.getBaselines());
			request.setAttribute("snapshotIDList",ConfigurationDAO.getSnapshotIDList());
			return super.ERROR;					
		}

		return super.SUCCESS;
	}
	
	public String compareBaselinesHome() throws Exception {
		
		try{
			
			request.setAttribute("baselinesList", ConfigurationDAO.getBaselines());
			
		}catch(Exception e){
			e.printStackTrace();
			request.setAttribute("msg", "Exception in compareBaselines. "+e.getMessage());
			return super.ERROR;
			
		}
		
		return super.SUCCESS;
	}
	
	
	public String compareBaselines() throws Exception {
		
		try{
			
			String baselineNames[] = request.getParameterValues("baselineName");
			
			Map baselineMap = BaselineDAO.getBaselinesNSnapshot(baselineNames);
			
			request.setAttribute("baselineMap", baselineMap);
			
		}catch(Exception e){
			e.printStackTrace();
			request.setAttribute("msg", "Exception in compareBaselines. "+e.getMessage());
			return super.ERROR;
			
		}
		
		return super.SUCCESS;
		
	}

}